//
//  HXDStatusCellTableViewCell.swift
//  weibo
//
//  Created by huxuedong on 15/12/9.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit
import SnapKit

let margin: CGFloat = 10
let nameFontSize: CGFloat = 15
let timeSourceFontSize: CGFloat = 10
let textFontSize: CGFloat = 15

class HXDStatusTableViewCell: UITableViewCell {

    private lazy var originalView: HXDStatusOriginalView = HXDStatusOriginalView()
    private lazy var toolView: HXDStatusToolView = HXDStatusToolView()
    private lazy var retweetView: HXDStatusRetweetView = HXDStatusRetweetView()
    //toolView顶部(top)的约束
    var toolViewTopConstraint: Constraint?
    var statusViewModel: HXDStatusViewModel? {
        didSet {
            originalView.statusViewModel = statusViewModel
            toolView.statusViewModel = statusViewModel
            //删除当前toolView顶部(top)的约束
            toolViewTopConstraint?.uninstall()
            if statusViewModel?.status?.retweeted_status != nil {
                retweetView.hidden = false
                retweetView.statusViewModel = statusViewModel
                toolView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            } else {
                retweetView.hidden = true
                toolView.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolViewTopConstraint = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolView)
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView).offset(8)
            make.trailing.equalTo(contentView)
        }
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(originalView)
            make.top.equalTo(originalView.snp_bottom)
            make.trailing.equalTo(originalView)
        }
        toolView.snp_makeConstraints { (make) -> Void in
            self.toolViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.equalTo(contentView)
            make.width.equalTo(contentView)
            make.height.equalTo(35)
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolView)
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
    }

}
